package com.threegroup.admin.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.threegroup.admin.entity.DishFlavorEntity;
import com.threegroup.common.utils.AssertUtil;
import com.threegroup.common.utils.Result;
import com.threegroup.common.utils.UserUtils;
import com.threegroup.admin.dao.AddressBookEntityMapper;
import com.threegroup.admin.entity.AddressBookEntity;
import com.threegroup.common.validator.AssertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AddressBookService {
    @Resource
    private AddressBookEntityMapper addressBookEntityMapper;

    /**
     * 添加地址
     * @param addressBookDTO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Result add(AddressBookEntity addressBookDTO) {
        setInfo(addressBookDTO);
        //非空判断
        notNull(addressBookDTO);
        //添加成功则count=1
        int count=addressBookEntityMapper.insert(addressBookDTO);
        AssertUtil.isTrue(count<0,"添加失败！");

        Result result=new Result();
        result.setMsg("添加成功！");
        result.setCode(0);
        result.setData(addressBookDTO);
        return result;
    }

    /**
     * 获取未填入对应值，并设置
     * id不用设置
     */
    private void setInfo(AddressBookEntity addressBookDTO) {

        //获取数据，设置对应值
        addressBookDTO.setIsDefault(0);

        addressBookDTO.setCreateDate(new Date());

        addressBookDTO.setUpdateDate(new Date());

        addressBookDTO.setIsDeleted(0);
    }

    /**
     * 非空判断
     * @param addressBookDTO
     */
    private void notNull(AddressBookEntity addressBookDTO) {
        //输入内容非空判断
        AssertUtil.isTrue(null==addressBookDTO,"请输入有效信息！");
        AssertUtil.isTrue(null==addressBookDTO.getConsignee(),"请输入姓名！");
        AssertUtil.isTrue(null==addressBookDTO.getGender(),"请选择性别！");
        AssertUtil.isTrue(null==addressBookDTO.getPhone(),"请输入电话！");
        AssertUtil.isTrue(null==addressBookDTO.getDetail(),"请输入地址！");

        //判断未输入内容非空
        AssertUtil.isTrue(null==addressBookDTO.getUserId()
                ||null==addressBookDTO.getUpdater()||null==addressBookDTO.getCreator(),"请输入有效userId！");
        AssertUtil.isTrue(null==addressBookDTO.getCreateDate(),"请输入创建时间！");
        AssertUtil.isTrue(null==addressBookDTO.getUpdateDate(),"请输入修改时间！");
        AssertUtil.isTrue(null==addressBookDTO.getIsDeleted(),"请输入是否删除！");
    }



    /**
     * 修改地址
     * @param addressBookDTO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Result update(AddressBookEntity addressBookDTO) {

        addressBookDTO.setCreateDate(new Date());
        addressBookDTO.setUpdateDate(new Date());

        //添加成功则count=1
        int count=addressBookEntityMapper.updateByPrimaryKey(addressBookDTO);
        AssertUtil.isTrue(count<0,"修改失败！");

        Result result=new Result();
        result.setMsg("修改成功！");
        result.setCode(0);
        result.setData(addressBookDTO);
        return result;
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Result delete(Long[] ids) {
        AssertUtil.isTrue(null==ids,"请输入有效id！");
        for (Long id : ids) {
            //删除成功则count=1
            int count = addressBookEntityMapper.deleteByPrimaryKey(id);
            AssertUtil.isTrue(count < 0, "删除失败！");
        }
        Result result=new Result();
        result.setMsg("删除成功！");
        result.setCode(0);
        return result;
    }


    public Result<PageInfo<AddressBookEntity>> getPageList(Integer limit1, Integer page1, String order1, String orderField1,String id1) {
        Result<PageInfo<AddressBookEntity>> pageInfoResult = new Result<>();
        //校验参数
        Integer limit;
        Integer page;
        String order;
        String orderField;
        //校验
        limit = (limit1 != null) ? limit1 : 10;//默认10
        page = (page1 != null) ? page1 : 1;//默认第1页
        order = (StringUtils.isBlank(order1)) ? "asc" : order1;//默认
        orderField = (StringUtils.isBlank(orderField1)) ? "consignee" : orderField1;//默认排序字段
        //开启分页
        PageHelper.startPage(page,limit);
        if (id1=="") {
            //获取list集合
            List<AddressBookEntity> addressBookEntityList =  addressBookEntityMapper.queryAll(order,orderField);
            PageInfo<AddressBookEntity> addressBookEntityPageInfo = new PageInfo<>(addressBookEntityList);
            //设置值
            pageInfoResult.setData(addressBookEntityPageInfo);
        }else {
            Long id=Long.valueOf(id1);
            //获取list集合
            List<AddressBookEntity> addressBookEntityList =  addressBookEntityMapper.selectByPrimaryKey(id);
            PageInfo<AddressBookEntity> addressBookEntityPageInfo = new PageInfo<>(addressBookEntityList);
            //设置值
            pageInfoResult.setData(addressBookEntityPageInfo);
        }


        return pageInfoResult;
    }

    public Result info(Long id) {
        Result result=new Result();
        result.setCode(0);
        result.setData(addressBookEntityMapper.info(id));
        return result;
    }
}
